package org.com.coolfish.service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.com.coolfish.common.database.entity.KuyuAddPackage;
import org.com.coolfish.common.database.entity.KuyuOperator;
import org.com.coolfish.common.database.entity.KuyuPackage;
import org.com.coolfish.common.database.entity.KuyuPackageList;
import org.com.coolfish.common.database.service.KuyuAddPackageService;
import org.com.coolfish.common.database.service.KuyuOperatorService;
import org.com.coolfish.common.database.service.KuyuPackageListService;
import org.com.coolfish.common.database.service.KuyuPackageService;
import org.com.coolfish.common.message.MsisdnCardMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
 * 
 * @author HS
 *	订购操作
 */
@Service
public class OrderPackageService {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private KuyuPackageService kuyuPackageService;
	@Autowired
	private KuyuAddPackageService kuyuAddPackageService;
	@Autowired
	private KuyuOperatorService kuyuOperatorService;
	@Autowired
	private KuyuPackageListService kuyuPackageListService;


	/**
	 * @return 返回指定月份的最后一天的59分59秒
	 */
	public static Date getLastMonthOfDate(Date date) {
		Calendar para = Calendar.getInstance(java.util.Locale.CHINA);
		para.setTime(date);
		para.set(Calendar.DATE, para.getActualMaximum(Calendar.DAY_OF_MONTH));
		para.set(Calendar.HOUR_OF_DAY, 23);
		para.set(Calendar.MINUTE, 59);
		para.set(Calendar.SECOND, 59);
		return para.getTime();
	}

	
	public void onMonth(MsisdnCardMessage message) {
		KuyuOperator operator = kuyuOperatorService.findOperatorLid(message.getOperatorid());
		logger.info("号码[{}],获取Operator的lid信息[{}]：", message.getTel(),operator.getLid());
		KuyuPackageList packageList = kuyuPackageListService.getPackageList(operator.getLid());
		logger.info("号码[{}],获取packageList信息[{}]去判断订购月份：", message.getTel(),packageList);
		KuyuPackage kuyuPackage = kuyuPackageService.getPropert(message.getPackageid());
		if (null != packageList ) {
			// 当月有效
			if (packageList.getType() == 1) {
				
				saveSameMonthOrder(message, operator, kuyuPackage);
				
			//次月订购
			}else if (packageList.getType() == 2) {
				
				saveNextMontOrder(message, operator, kuyuPackage);
				
			}else {
				accumulativePackage(message, operator, kuyuPackage);
				
			}
		
		}
		

	}
	
	
	
	

	/**累计套餐
	 * 
	 * @param message
	 * @param operator
	 * @param kuyuPackage
	 */
	private void accumulativePackage(MsisdnCardMessage message, KuyuOperator operator, KuyuPackage kuyuPackage) {
		KuyuAddPackage kuyu;
		kuyu = new KuyuAddPackage();
		
		kuyu.setCard(message.getTel());
		kuyu.setCardId(message.getCardid());
		kuyu.setDid(0);
		kuyu.setFinalprice(kuyuPackage.getPrice());
		kuyu.setOperatorName(operator.getName());
		kuyu.setPackageid(kuyuPackage.getId());
		kuyu.setPid(0);
		kuyu.setProfit(BigDecimal.valueOf(0.00));
		kuyu.setRemarks(" ");
		kuyu.setResult("订购成功");
		long addTime = System.currentTimeMillis();
		kuyu.setPackage1(kuyuPackage.getName());
		kuyu.setAddtime(new Date(addTime));
		//String starttime = sdf.format(addTime);
		kuyu.setStarttime(kuyu.getAddtime());
		//时间累加
		
		KuyuPackage findPackagePropert = kuyuPackageService.findPackagePropert(message.getPackageid());
		Integer usetime = findPackagePropert.getUsetime();
		
		Date plusDay = plusDay(usetime,kuyu.getAddtime());
 
		if(null == plusDay) {
			
			
		} 
		kuyu.setEndtime(plusDay);
		kuyu.setStatus(2);
		kuyu.setSumflow(kuyuPackage.getFlow());
		kuyu.setType(1);
		kuyu.setUid(50);
		kuyu.setUseflow(BigDecimal.valueOf(0));
		kuyu.setSerialNumber(" ");
		kuyu.setSource(" ");
		if (findPackagePropert.getUsetime() == 90) {
			//订购一条季包
			kuyuAddPackageService.save(kuyu);
			//分别生成3条对应的月包
			//当月
			saveSameMonthOrder(message,operator,kuyuPackage);
			//次月
			saveNextMontOrder(message,operator,kuyuPackage);
			//次次月
			saveSecondMonthOrder(message, operator, kuyuPackage);
			
		}else {
			kuyuAddPackageService.save(kuyu);
		}
	}
	
	//季包
	private void seasonPackage(MsisdnCardMessage message, KuyuPackage kuyuPackage, KuyuOperator operator) {
		KuyuAddPackage kuyu;
		kuyu = new KuyuAddPackage();
		
		kuyu.setCard(message.getTel());
		kuyu.setCardId(message.getCardid());
		kuyu.setDid(0);
		kuyu.setFinalprice(kuyuPackage.getPrice());
		kuyu.setOperatorName(operator.getName());
		kuyu.setPackageid(kuyuPackage.getId());
		kuyu.setPid(0);
		kuyu.setProfit(BigDecimal.valueOf(0.00));
		kuyu.setRemarks(" ");
		kuyu.setResult("订购成功");
		long addTime = System.currentTimeMillis();
		kuyu.setPackage1(kuyuPackage.getName());
		kuyu.setAddtime(new Date(addTime));
		//String starttime = sdf.format(addTime);
		kuyu.setStarttime(kuyu.getAddtime());
		//时间累加
		
		KuyuPackage findPackagePropert = kuyuPackageService.findPackagePropert(message.getPackageid());
		Integer usetime = findPackagePropert.getUsetime();
		
		Date plusDay = plusDay(usetime,kuyu.getAddtime());
 
		if(null == plusDay) {
			
			
		} 
		kuyu.setEndtime(plusDay);
		kuyu.setStatus(2);
		kuyu.setSumflow(kuyuPackage.getFlow());
		kuyu.setType(1);
		kuyu.setUid(50);
		kuyu.setUseflow(BigDecimal.valueOf(0));
		kuyu.setSerialNumber(" ");
		kuyu.setSource(" ");
		kuyuAddPackageService.save(kuyu);
	}
	/*
	 * 次次月订单生成
	 */
	private void saveSecondMonthOrder(MsisdnCardMessage message, KuyuOperator operator,
			KuyuPackage kuyuPackage) {
		KuyuAddPackage kuyu;
		kuyu = new KuyuAddPackage();
		
		kuyu.setCard(message.getTel());
		kuyu.setCardId(message.getCardid());
		kuyu.setDid(0);
		kuyu.setFinalprice(kuyuPackage.getPrice());
		kuyu.setOperatorName(operator.getName());
		kuyu.setPackageid(kuyuPackage.getId());
		kuyu.setPid(0);
		kuyu.setProfit(BigDecimal.valueOf(0.00));
		kuyu.setRemarks(" ");
		kuyu.setResult("订购成功");
		long addTime = System.currentTimeMillis();
		kuyu.setPackage1(kuyuPackage.getName());
		kuyu.setAddtime(new Date(addTime));
		kuyu.setStarttime(TimeService.SecondMonthFirstDay());
		kuyu.setEndtime(TimeService.SecondMonthLastDay());
		kuyu.setStatus(2);
		kuyu.setSumflow(kuyuPackage.getFlow());
		kuyu.setType(2);
		kuyu.setUid(50);
		kuyu.setUseflow(BigDecimal.valueOf(0));
		kuyu.setSerialNumber(" ");
		kuyu.setSource(" ");
		kuyuAddPackageService.save(kuyu);
	}
	/*
	 * 次月订单生成
	 */
	private void saveNextMontOrder(MsisdnCardMessage message, KuyuOperator operator, KuyuPackage kuyuPackage) {
		KuyuAddPackage kuyu;
		kuyu = new KuyuAddPackage();
		
		kuyu.setCard(message.getTel());
		kuyu.setCardId(message.getCardid());
		kuyu.setDid(0);
		kuyu.setFinalprice(kuyuPackage.getPrice());
		kuyu.setOperatorName(operator.getName());
		kuyu.setPackageid(kuyuPackage.getId());
		kuyu.setPid(0);
		kuyu.setProfit(BigDecimal.valueOf(0.00));
		kuyu.setRemarks(" ");
		kuyu.setResult("订购成功");
		long addTime = System.currentTimeMillis();
		kuyu.setPackage1(kuyuPackage.getName());
		kuyu.setAddtime(new Date(addTime));
		kuyu.setStarttime(TimeService.nextMonthFirstDay());
		kuyu.setEndtime(TimeService.nextMonthLastDay());
		kuyu.setStatus(2);
		kuyu.setSumflow(kuyuPackage.getFlow());
		kuyu.setType(2);
		kuyu.setUid(50);
		kuyu.setSerialNumber(" ");
		kuyu.setSource(" ");
		kuyu.setUseflow(BigDecimal.valueOf(0));
		kuyuAddPackageService.save(kuyu);
	}
	/*
	 * 本月订单生成
	 */
	private void saveSameMonthOrder(MsisdnCardMessage message, KuyuOperator operator, KuyuPackage kuyuPackage) {
		KuyuAddPackage kuyu;
		kuyu = new KuyuAddPackage();
		
		kuyu.setCard(message.getTel());
		kuyu.setCardId(message.getCardid());
		kuyu.setDid(0);
		kuyu.setFinalprice(kuyuPackage.getPrice());
		kuyu.setOperatorName(operator.getName());
		kuyu.setPackageid(kuyuPackage.getId());
		kuyu.setPid(0);
		kuyu.setProfit(BigDecimal.valueOf(0.00));
		kuyu.setRemarks(" ");
		kuyu.setSource(" ");
		kuyu.setResult("订购成功");
		long addTime = System.currentTimeMillis();
		kuyu.setPackage1(kuyuPackage.getName());
		kuyu.setAddtime(new Date(addTime));
		kuyu.setEndtime(TimeService.monthLastDay());
		//kuyu.setStarttime();
		kuyu.setStatus(2);
		kuyu.setSumflow(kuyuPackage.getFlow());
		kuyu.setType(3);
		kuyu.setUid(50);
		kuyu.setSerialNumber(" ");
		kuyu.setUseflow(BigDecimal.valueOf(0));
		kuyuAddPackageService.save(kuyu);
	}
	
	public static String plusDay1(int num,String newDate) throws ParseException{
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date  currdate = format.parse(newDate);
        System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        System.out.println("增加天数以后的日期：" + enddate);
        return enddate;
    }

	
	public static Date plusDay(Integer usetime,Date newDate) {
		if (usetime != null && usetime > 0) {
            long apartTime = usetime * 24 * 60 * 60 * 1000L;
            return new Date(newDate.getTime()+apartTime);	
            }
        return null;
    }


	/*
	 * 根据属性固定包和叠加包去判断累计长周期订购
	 */
	public void saveCycleWithPackageProperty(MsisdnCardMessage message) {
		// 获取套餐包固定和叠加属性
		KuyuPackage kuyuPackage = kuyuPackageService.findPackagePropert(message.getPackageid());
		KuyuOperator operator = kuyuOperatorService.findOperatorLid(message.getOperatorid());

		// 有固定套餐包,没有叠加包的情况
		if (kuyuPackage.getBase() == 2 && kuyuPackage.getStackable() == 1) {
			// 空套餐+1, 当月套餐+1次月生效, 如果是有次月订购(下月1日月套餐+1)
			if (kuyuPackage.getUsetime() == 30) {
				saveSameMonthOrder(message, operator, kuyuPackage);
			}else if(kuyuPackage.getUsetime() > 30 && kuyuPackage.getUsetime() < 61) {
				saveNextMontOrder(message, operator, kuyuPackage);
			}
			
			
			// 没有固定包,有叠加包的情况
		} else if (kuyuPackage.getBase() == 1 && kuyuPackage.getStackable() == 2) {
			// 叠加 充几个加几个 当月生效 如订购的是次月再去加次月
			if (kuyuPackage.getUsetime() == 30) {
				saveSameMonthOrder(message, operator, kuyuPackage);
			} else if(kuyuPackage.getUsetime() > 30 && kuyuPackage.getUsetime() < 61){
				saveNextMontOrder(message, operator, kuyuPackage);
			}
			
			// 既没有固定包也有没叠加包的情况
		} else if (kuyuPackage.getBase() == 1 && kuyuPackage.getStackable() == 1) {
			if (kuyuPackage.getUsetime() == 90) {
				//季包
				seasonPackage(message, kuyuPackage, operator);
				//分别生成3条对应的月包
				//当月
				saveSameMonthOrder(message,operator,kuyuPackage);
				//次月
				saveNextMontOrder(message,operator,kuyuPackage);
				//次次月
				saveSecondMonthOrder(message, operator, kuyuPackage);
				
			}else {
				
				seasonPackage(message, kuyuPackage, operator);
			}
			
			
		} 
	}



}
